<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BookCate extends Model
{
    use HasFactory;
    protected $table = 'book_cate';

    //递归获取书籍类别
    public static function getBookCateByPid($pid)
    {
        $data = DB::table("book_cate")
            ->where('pid',$pid)
            ->get();
        $res  = [];
        //遍历 递归
        foreach ($data as $key => $value) {
            // 代码段：核心点
            $value->child = self::getBookCateByPid($value->id);
            $res[]         = $value;
        }
        return $res;
    }
}
